package org.pubflex{
    import component.*;
    
    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    
    import mx.controls.LinkButton;
    import mx.managers.PopUpManager;
        

    public class Keybindings extends EventDispatcher{
 
        private static var bindings:Keybindings; 

        public function Keybindings(){
			super();									    		
		}

        public static function newInstance(display:DisplayObject):Keybindings{
            trace(display)
            if(bindings == null){
                bindings = new Keybindings();
                display.addEventListener(KeyboardEvent.KEY_DOWN, bindings.handleKeyEvent);
            }
            return bindings; 
        }
        
        public function handlePostComplete(e:Event):void{
            trace(e);
            trace (this.hasEventListener(e.type))
            trace (this.hasEventListener(MouseEvent.CLICK))
            //dispatchEvent(new Event(MouseEvent.CLICK,true));
            dispatchEvent(new Event(NewEntryBase.POST_COMPLETE,true,false));
        }
        
        public function handleKeyEvent(e:KeyboardEvent, caller:DisplayObject):void{
            namespace ATOM = "http://www.w3.org/2005/Atom"
            namespace APP = "http://www.w3.org/2007/app";
            namespace XML_NS = "http://www.w3.org/XML/1998/namespace";
            use namespace APP;
            use namespace XML_NS;
            //use namespace ATOM;
            // CTRL + N == New Entry
            
            trace(e.keyCode);
            
            // ENTER 
            if(e.keyCode == 13){
               var click:MouseEvent = new MouseEvent(MouseEvent.CLICK, true);                            
                
               if(e.target is LinkButton){
                    LinkButton(e.target).dispatchEvent(click);
               }
            }
            
            if(e.ctrlKey == true  && e.keyCode == 78){

		        var myParent:DisplayObject = caller.parent as DisplayObject;                 
				var entry:newEntry = newEntry(PopUpManager.createPopUp(myParent, newEntry , true));
                entry.addEventListener(NewEntryBase.POST_COMPLETE, handlePostComplete);
                        
                var parent:BrowserBarBase = caller as BrowserBarBase;
                
                var service:XML = parent.serviceDocument; 
                // XXX This really ought not be here. 
                trace(parent.serviceDocument.@base)
                entry.baseURI = parent.serviceDocument.@base;
                
                trace("Collections " + service.collection)
                trace("Collections " + service.workspace.collection)
                trace("Collections " + service.workspace.collection.ATOM::title)
                trace("Collections " + service.workspace.collection.ATOM::title.text())
				
				PopUpManager.centerPopUp(entry);

                
                // = service.workspace.collection; 
                var cols:XMLList = service.workspace.collection;
                
                var collectionLabels:Array = new Array();
                var col:XML; 
                for each (col in cols){                  
                    var title:LinkButton = new LinkButton(); 
                    trace("Title : " + col.ATOM::title.text());

                    title.data  = col;
                    title.label  = col.ATOM::title.text();   
                    collectionLabels.push(title);
                }
                entry.collectionsComboBox.dataProvider = collectionLabels;

 		    }      
        }
    }
}